

// 146.LRU 缓存
class LRUCache {
    int capacity_;
    list<pair<int , int>> l ;
    unordered_map<int, list<pair<int,int>>::iterator> m;   
public:
    LRUCache(int capacity) 
    :capacity_(capacity)
    {}
    
    int get(int key) {
        if(!m.count(key)) return -1;
        auto& it = m[key];
        l.splice(l.begin() , l , it);
        return it->second;
    }
    
    void put(int key, int value) {
        if(m.count(key))
        {
            get(key);                   // 提到最前面来
            l.front().second = value;   // 修改值
            return ;
        }

        if(l.size() == capacity_)       // 空间满了
        {
            m.erase(l.back().first);
            l.pop_back();
        }
        l.push_front({key , value});
        m[key] = l.begin();
    }
};

/**
 * Your LRUCache object will be instantiated and called as such:
 * LRUCache* obj = new LRUCache(capacity);
 * int param_1 = obj->get(key);
 * obj->put(key,value);
 */